/**
 * 客户价值评估 - 编辑页面
 * @author liuw
 * @date 2019/11/18
 */
define(function () {
    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            form: {
                GWBY002_NAME:null,
                GWBYORIU_NAME:null,
                GWBYORIG_NAME:null
            },
            selectOptions: {

            },
            disabled:false,
            rules : {
                GWBY002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
            },
        },
        listData = [],
        questionlist = app.datas_("gwby_questionList"),
        cols = [[
            { field: "GWBY006", title: "评估项", width: 150},
            { field : "GWBY007" , title: "评估值", width : 150
            ,templet: function(d){
                    let selectOptions = app.datas_("gwby007_" + d.code);
                    let optionList = '';
                    for (let i = 0; i < selectOptions.length; i++) {
                        let GWBY007_NUM = parseInt(d.GWBY007);
                        if(d.GWBY007!=undefined && d.GWBY007!=='' && (i+1) === GWBY007_NUM){
                            optionList += '<option selected="selected" value='+selectOptions[i].code+'>'+selectOptions[i].value + '</option> \n';
                        }else {
                            optionList += '<option value='+selectOptions[i].code+'>'+selectOptions[i].value + '</option> \n';
                        }
                    }
                    return '<select lay-filter="gwby007Select">' + optionList +
                        '</select>'
                }
            },
            { field : "GWBY008" , title: "得分", width : 50}

        ]],
        callObj,
        pageVue,
        detailReadOnly=false,
        moduleName = "客户价值评估信息",
        table, form,gwai305EditTable,layOpenIndex
    ;

    var Gwai305Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai305_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                form = layui.form;
                gwai305EditTable = table.render({
                    elem: app.getByid(doc, 'gwai305_edit_table'),
                    id: 'gwai305_edit_table',
                    height:'360',
                    cols: cols,
                    data: listData,
                    done: function (res, curr, count) {
                        var tableElem = $("#gwai305_edit_table").next('.layui-table-view');
                        count || tableElem.find('.layui-table-header').css('overflow', 'auto');
                        layui.each(tableElem.find('select'), function (index, item) {
                            var elem = $(item);
                            elem.val(elem.data('value')).parents('div.layui-table-cell').css('overflow', 'visible');
                            elem.next().children().eq(0).children().first().css({"height":"20px","border":"none"})
                        });
                    }
                });

                //监听单元格编辑
                form.on('select(gwby007Select)',function (data) {
                    var tdElem = $(data.othis).parents('div.layui-table-cell').parents('td').parents('tr');
                    var tdNum = parseInt(tdElem.data().index); // 行号
                    listData[tdNum].GWBY007 = data.value;

                    var gwby008Option = app.datas_("gwby008_" + (tdNum+1));
                    var oldScore = listData[tdNum].GWBY008===''?0:parseInt(listData[tdNum].GWBY008);
                    var newScore = parseInt(gwby008Option[parseInt(data.value)-1].value);
                    vueData.form.GWBY003 += (newScore-oldScore);
                    listData[tdNum].GWBY008 = newScore;

                    var scoreElem = $(data.othis).parents('div.layui-table-cell').parents('td').next();
                    scoreElem.children()[0].innerText = newScore;
                    // self.tableReload(listData);
                });
            });
        },

        changeSelectStyle: function() {
            var tableElem = $("#gwai305_edit_table").next('.layui-table-view');
            layui.each(tableElem.find('select'), function (index, item) {
                var elem = $(item);
                elem.next().children().eq(0).children().first().css({"height":"20px","border":"none"})
            });
        },

        tableReload: function (listData) {
            self._reloadTable({id: 'gwai305_edit_table', data: listData});
        },

        addData: function (listData) {
            app.ajax({
                xurl: 'crm/gwai305/add',
                xdata: listData,
                loading: true,
                toJsonStr: true,
                okmsg: '客户价值评估信息增加成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai305').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        editData: function (listData) {
            app.ajax({
                xurl: 'crm/gwai305/edit',
                xdata: listData,
                loading: true,
                toJsonStr: true,
                okmsg: '客户价值评估信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai305').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (tagData) {
            pageVue.$refs.form.resetFields();
            listData = [];
            layOpenIndex = tagData.layOpenIndex;
            if (tagData.is_add) {
                vueData.isAdd = true;
                app.ajax({
                    xurl: 'crm/gwai305/getAddData',
                    cb: function (result) {
                        if (result.ok) {
                            var resData = result.data.data;
                            app.extendUpLow({
                                target: vueData.form,
                                res: {
                                    GWBY001: resData.ID,
                                    GWBYORIU: resData.CURRENT_USER_ID,
                                    GWBYORIU_NAME: resData.CURRENT_USER_NAME,
                                    GWBYORIG: resData.CURRENT_ORG,
                                    GWBYORIG_NAME: resData.CURRENT_ORG_NAME,
                                    GWBYDATE: resData.CURRENT_DATE,
                                    GWBY003: 0,
                                    GWBYACTI : "N"
                                },
                                reset: true
                            });

                        }
                    }
                });
                listData.pushArray(app.cloneJSON(questionlist).addMap({GWBY008:''}));
                self.tableReload(listData);
                form.render('select');
                self.changeSelectStyle();
            } else {
                vueData.disabled = false;
                vueData.isAdd = false;
                callObj = tagData.obj;

                listData.pushArray(app.cloneJSON(questionlist));
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });

                detailReadOnly = tagData.detailReadonly;
                if(!detailReadOnly && (vueData.form.GWBYACTI==="N" || vueData.form.GWBYACTI==="R")){
                    vueData.disabled = false;
                }else {
                    vueData.disabled = true;
                }
                app.ajax({
                    xurl: 'crm/gwai305/getDetailList',
                    xdata: {GWBY001: callObj.data.GWBY001},
                    loading: true,
                    cb: function (result) {
                        if(result.ok) {
                            let data = result.data.data;
                            for (let i = 0; i < data.length; i++) {
                                listData[i].GWBY007 = data[i].GWBY007;
                                listData[i].GWBY008 = data[i].GWBY008;
                            }
                            self.tableReload(listData);
                        }
                    }
                });
            }
        },

        _ready: function () {
            table = layui.table;
            vueData.form = app.fields_(['GWBY_T'],vueData.form);
            pageVue = new Vue({
                el: '#gwai305_edit',
                data: vueData,
                methods: {
                    cancle: function () {
                        self._closeTpl();
                    },
                    saveData: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                var listData = gwai305EditTable.getDatas();
                                var canSave = true;
                                for (let i = 0; i < listData.length; i++) {
                                    if(listData[i].GWBY008 === '') {
                                        canSave = false;
                                        app.showWarning("表格选项没有填完整");
                                        return;
                                    }
                                }
                                if(!canSave) {
                                    return;
                                }
                                listData.forEach(v => v.GWBY006 = v.GWBY006.substring(0,1));

                                listData.addMap({
                                  GWBY001 : vueData.form.GWBY001 ,
                                  GWBY002 : vueData.form.GWBY002 ,
                                  GWBY003 : vueData.form.GWBY003 ,
                                  GWBYDATE : vueData.form.GWBYDATE ,
                                  GWBYORIU : vueData.form.GWBYORIU,
                                  GWBYORIG : vueData.form.GWBYORIG,
                                  GWBY004 : vueData.form.GWBY004 ,
                                  GWBY005 : vueData.form.GWBY005,
                                  GWBYACTI: vueData.form.GWBYACTI
                                },true);
                                if (vueData.isAdd) {
                                    self.addData(listData);
                                } else {
                                    self.editData(listData);
                                }
                            }
                        });
                    },

                    // 客户开窗
                    openGwakPage:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwak001',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWBY002 = data.GWAK001;
                                    vueData.form.GWBY002_NAME = data.GWAKL004;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择客户',
                                area: ['600px', '465px']
                            }
                        });
                    },

                },
                created: function () {
                    self._setEleIsReady('gwai305_edit');
                }
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwai305Edit();

})
;